﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SVAP
{
    public partial class ResultConfigForm : Form
    {
        private static string strExePath = "";//应用程序所在路径

        private static string strSourcePath;//配置文件路径

        public ResultConfigForm()
        {
            InitializeComponent();

            //将该窗口设置为程序的焦点，且不允许改变，直至关闭该窗口
            //SetWindow setWin = new SetWindow(this.Handle);
            //setWin.Star();
        }

        //关闭窗体
        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ResultConfigForm_Load(object sender, EventArgs e)
        {
            //获得".exe"所在路径
            strExePath = System.AppDomain.CurrentDomain.BaseDirectory;
            strSourcePath = strExePath + @"ResultConfig.txt";
            StreamReader sr = new StreamReader(strSourcePath, System.Text.Encoding.Default);
            string sLine = "";
            while (sLine != null)
            {
                sLine = sr.ReadLine();
                if (sLine != null&&sLine!="")
                    listBox_showResult.Items.Add(sLine);
            }
            sr.Close();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            AddConfigResult acr = new AddConfigResult();
            acr.Owner = this;
            acr.Show();

            //listBox_showResult.Items.Clear();

            //StreamReader sr = new StreamReader(strSourcePath, System.Text.Encoding.Default);
            //string sLine = "";
            ////sLine = sr.ReadLine();
            //while (sLine != null)
            //{
            //    sLine = sr.ReadLine();
            //    if (sLine != null)
            //        listBox_showResult.Items.Add(sLine);
            //}
            //sr.Close();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            string tempCon = listBox_showResult.SelectedItem.ToString();
            if (tempCon!="车辆数目"&&tempCon!="车辆颜色")
            {
                //从列表中删除
                listBox_showResult.Items.RemoveAt(listBox_showResult.SelectedIndex);
                //从文件中删除
                string strConfigFilePath = System.AppDomain.CurrentDomain.BaseDirectory;// set destination path
                strConfigFilePath += @"ResultConfig.txt";
                List<string> tempList = new List<string>();
                StreamReader sr = new StreamReader(strConfigFilePath, Encoding.Default);
                string sLine = "";
                sLine = sr.ReadLine();
                tempList.Add(sLine);
                while (sLine != null)
                {
                    sLine = sr.ReadLine();
                    if (sLine != null && sLine != "")
                        tempList.Add(sLine);
                }
                sr.Close();
                // 从list中删除要删除的
                tempList.Remove(tempCon);

                //写回文件
                FileStream fs = new FileStream(strConfigFilePath, FileMode.Create);
                //fs.SetLength(0);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);

                foreach (string str in tempList)
                {
                    sw.WriteLine(str);
                }
                sw.Close();
            }
            
        }
    }
}
